package com.fujimana.rockincat.restree.init.method;

import com.fujimana.rockincat.restree.annotation.http.WebMethod;
import com.fujimana.rockincat.restree.init.Handler;
import com.fujimana.rockincat.restree.init.Item;
import com.fujimana.rockincat.restree.util.ClassUtils;

public class MethodHandler implements Handler<Item> {

	@Override
	public Item handle(String resource) {
		Class<?> clazz = ClassUtils.loadClass(resource);
		
		WebMethod method = clazz.getAnnotation(WebMethod.class);
		
		return new MethodItem(clazz, method.value());
	}

	@Override
	public boolean isHandle(String resource) {

		if (!resource.endsWith(".class")) {
			return false;
		}
		
		Class<?> clazz = ClassUtils.loadClass(resource);
		
		if(clazz == null) {
			return false;
		}	

		return clazz.getAnnotation(WebMethod.class) != null;
	}

}
